package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.LunchBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.LunchMapper;
import com.java2207.group1.pojo.Lunch;
import com.java2207.group1.service.LunchService;
import com.java2207.group1.vo.Breakfast.BarVo;
import com.java2207.group1.vo.Breakfast.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author AL 2022/7/26  20:50
 */
@Service("lunchService")
public class LunchServiceImpl implements LunchService {

    @Autowired
    private LunchMapper lunchMapper;


    @Override
    public int addLunch(Lunch lunch) {
        return lunchMapper.addLunch(lunch);
    }

    @Override
    public int deleteLunch(Integer id) {
        return lunchMapper.deleteLunch(id);
    }

    @Override
    public int updateLunch(Lunch lunch) {
        return lunchMapper.undateLunch(lunch);
    }

    @Override
    public LayuiPageVo<Lunch> lunchPageDate(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        //  分页的数据
        PageHelper.startPage(page,limit);
        List<Lunch> lunches = lunchMapper.selectByExample(params);

        //  mybatis 的分页数据结构
        PageInfo<Lunch> lunchPageInfo = new PageInfo<>(lunches);

        // 把 mybatis 的分页数据封装到 layui 中
        LayuiPageVo<Lunch> vo = new LayuiPageVo<>();
        vo.setCode(0);
        vo.setMsg("分页列表数据");
        vo.setCount(lunchPageInfo.getTotal());
        vo.setData(lunchPageInfo.getList());
        return vo;
    }

    @Override
    public List<BarVo> nameBarData() {
        List<BarVo> list = lunchMapper.nameBarData();
        return list;
    }

    @Override
    public List<PieVo> namePieData() {
        List<PieVo> list = lunchMapper.namePieData();
        return list;
    }

    @Override
    public List<LunchBo> lunchExcelData(Map<String, Object> params) {
        List<Lunch> lunchs = lunchMapper.selectByExample(params);

        List<LunchBo> lunchBos =lunchs.stream().map((e) -> {
            LunchBo lunchBo = new LunchBo();
            lunchBo.setId(e.getId());
            lunchBo.setName(e.getName());
            lunchBo.setTime(e.getTime());
            lunchBo.setPic(e.getPic());
            lunchBo.setSpecial(e.getSpecial());
            return lunchBo;
        }).collect(Collectors.toList());
        return lunchBos;
    }
}
